/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: GatewayFilterConfiguration
 * Author:   Allen
 * Date:     2020/8/24 14:13
 * Description: 处理对swagger的过滤配置
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.gateway.config;

import com.bim.marvel.gateway.core.entity.AuthVerifyExclude;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈处理对swagger的过滤配置〉
 *
 * @author Allen
 * @date 2020/8/24
 * @since 1.0.0
 */
@Component
@Setter
@Getter
@ConfigurationProperties(prefix = "spring.cloud.gateway.auth-verify-filter")
public class GatewayFilterConfiguration {

    /**
     * 获得过滤列表数据
     */
    private List<AuthVerifyExclude> excludes = new ArrayList<>();

    /**
     * 启用Swagger标识
     */
    private boolean enableSwagger = false;

    /**
     * 自定义过滤排除列表的校验
     * @param path 路径
     * @return 校验结果
     */
    public AuthVerifyExclude getAuthVerifyExclude(String path) {
        if (!StringUtils.isEmpty(path)) {
            boolean swaggerVerify = enableSwagger && (path.toLowerCase().contains("swagger-ui.html")
                    || path.toLowerCase().contains("doc.html")
                    || path.toLowerCase().contains("/webjars/springfox-swagger-ui")
                    || path.toLowerCase().contains("/webjars/**"))
                    || path.toLowerCase().contains("/swagger-resources")
                    || path.toLowerCase().contains("/v2/api-docs");
            if (swaggerVerify) {
                return new AuthVerifyExclude();
            }
            if (excludes != null && excludes.size() > 0) {
                for (AuthVerifyExclude authVerifyExclude : excludes) {
                    if (path.toLowerCase().contains(authVerifyExclude.getUri().toLowerCase())) {
                        return authVerifyExclude;
                    }
                }
            }
        }
        return null;
    }

}
